(function ($) {
	var tdKey="text";
	/**
	  * 从那行开始扫描索引是row=0,column=0起
	  * r 行索引
	  * ca 列数组
	  */
	$.fn.toJsonByRColumnsArray=function(r,ca){
		//table对象
		obj = $(this);
		// 标题 对应  的索引json
		var caiJson = {};
		var cai = new Array();
		$.each(ca,function(i,d){
			//这个索引是从0开始
			var con = obj.find('td:contains('+d+')').index();
			 caiJson[con]=con;
			 cai[i]=con;
			});
			
	   var jt ={};
	   var jr = [];
	   var jd =  {};
	    obj.find("tr").each(function(i){
			//行大于传入的行开始遍历
			  if( i >= r)
			   { 
			    $(this).find("td").each(function(j){ 
			            var tdi = caiJson[j];
						 if(tdi !== undefined)
						 {
							 var o = $(this);
							// var rowspan = o.attr("rowspan");
							// var colspan = o.attr("colspan");
							 var text = getText(o);
							// jd["rowspan"] = rowspan;
							// jd["colspan"] = rowspan;
							 jd[tdKey] = text;
							
							 jr.push(jd);
					        jd={};
						 }
					 });    
                  jt[i]=jr;
		          jr = [];
			   } 
			});
			return jt;	
	};
	
	/**
	  * 从那行开始扫描索引是row=0,column=0起
	  * r 行索引
	  * ca 列数组
	  */
	$.fn.toArrayByRColumnsIndex=function(r,ca){
		//table对象
		obj = $(this);
        var tr = new Array();
        var cols;
        var rows;
        var colspan = 1;
        var rowspan = 1;
	    obj.find("tr").each(function(i){
	  	      //合并列的值
			  cols = $(this).find('td:first').attr('colspan');
			  rows = $(this).find('td:first').attr('rowspan');
			  if(cols !== 1 &&  cols !== undefined)
			  {
				  colspan = cols;
			  }
			  if(rows !== 1 &&  rows !== undefined)
			  {
				  rowspan = rows;
			  }
			  if( i >= r)//行大于传入的行开始遍历
			   { 
				  var m = i-r;
				  tr[m] = new Array();
			      for(var j = 0 ;j < ca.length ; j++)
			      {
			          var obj = $(this).find('td:eq('+ca[j]+')');
			          if(rows == 1 ||  rows == undefined)
			          {
			        	  obj = $(this).find('td:eq('+(ca[j]-colspan+1)+')');  
			          }
			          var text = getText(obj);
			          tr[m][j] = text; 	 
			      }
			   } 
			});
         return tr;
	};
	
	
	//
	//去标题行
	//转json
	//不带table样式
	//
	 $.fn.toJsonAllColumns = function(){
		 var jt ={};
	     var jr = [];
	     var jd =  {};
		 //遍历tr
		 $(this).find("tr").each(function(i){
			    //去的标题行
			   if(i != 0)
			   {
			      //遍历td
				   $(this).find("td").each(function(){
					   var text = getText($(this));
					   jd[tdKey] = text;
					   jr.push(jd);
					   jd = {};
				   });
				   jt[i] = jr;
				   jr = [];
			   }   
	      });
		 return jt;
      };
      /**
       * 获取td的内容
       * td内容可能为input 或者 a span  div
       */
	  function getText(obj)
	  {
		  var reg1 = /^input|INPUT/;
		  var reg2 =/^a|span|div/;
		  var td =  obj.html();
		  var t;
		  if(reg1.test(td)){
			 t = obj.children(":first").val();
		  }else if(reg2.test(td)){
			 t = obj.children(":first").text();
		  }else{
			 t=  obj.text();
			  }
		  return t;	  
	  }	
	/**
	 * json
	 * 不带table样式
	 *第一行不取值
	 */
    $.fn.toTwoDimensionArray = function(){
		   //定义一维数组
	       var tr = new Array();
		   var jr={};
		   var jd=[];
		   //遍历tr
		   $(this).find("tr").each(function(i){
				 //去的标题行
			   if(i != 0)
			   {
				   var m = i-1;
				   //定义二位数组
				   tr[m] = new Array();
				   //遍历td
				   $(this).find("td").each(function(j){
					    var td =  $(this).html();
						//匹配input
						var reg = /^input|INPUT/;
						if(reg.test(td))
						{
							var tdv =  $(this).children(":first").val();
							tr[m][j] = tdv;
						}else{
							tr[m][j] = $(this).text();
						}
						
				   });
			   }
			});
		   return tr;      
    };
	
})(jQuery);
/**
 导出Excel
*/
function expExcel(url,tabJson,othJson,name,bcName)
{
	expAjax(url, tabJson, othJson, name, bcName, '');
}
 /**
 导出Excel
*/
function expExcelByRow(url,tabJson,othJson,name,brName)
{
	expAjax(url, tabJson, othJson, name, '', brName);

}

 function expAjax(url,tabJson,othJson,name,bcName,brName)
 { 
	 $.ajax({
		   type: "POST",
		   url: url,
		   data: {tabJson:tabJson,name:name,othJson:othJson,brName:brName,bcName:bcName},
		   dataType:"json",
		   success: function(msg){
			   if(msg.OK != null && msg.OK!='' && msg.OK==1)
			   {
				   window.location = ("../expexcel/expexcel!downloadXls.action?name="+msg.name);
			   };
		   },
		   error:function(XMLHttpRequest, textStatus, errorThrown)
		   {
			   alert("导出失败！");
		   }
	 });
}